package com.angel.resourceBundle.propertyValue.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import com.angel.architecture.persistence.base.PersistentObject;
import com.angel.resourceBundle.internationalizable.Internationalizable;
import com.angel.resourceBundle.internationalizer.Internationalizer;
import com.angel.resourceBundle.propertyValue.PropertyValue;


/**
 * If you want to implements a new class to property value object, yo must inherit {@link ComplexPropertyValue}.
 *
 * @author William
 * @since 10/November/2007
 */
@Entity
public class ComplexPropertyValue extends PersistentObject implements PropertyValue {
	private static final long serialVersionUID = -8446976132988816400L;

	@Column(nullable = false, length = 60, updatable = false, unique = true)
	private String extendedKey;

	@ManyToOne(cascade = CascadeType.ALL, optional = false)
	private Internationalizer internationalizer;

	@ManyToOne(cascade = CascadeType.ALL, optional = false)
	private Internationalizable internationalizable;

	public ComplexPropertyValue(){
		super();
	}

	public String getText(){
		return this.getInternationalizable().getText();
	}

	public ComplexPropertyValue(Internationalizer internationalizer, Internationalizable internationalizable){
		this();
		this.internationalizable = internationalizable;
		this.internationalizer = internationalizer;
	}

	/**
	 * @return the internationalizer
	 */
	public Internationalizer getInternationalizer() {
		return internationalizer;
	}

	/**
	 * @param internationalizer the internationalizer to set
	 */
	public void setInternationalizer(Internationalizer internationalizer) {
		this.internationalizer = internationalizer;
	}

	/**
	 * @return the internationalizable
	 */
	public Internationalizable getInternationalizable() {
		return internationalizable;
	}

	/**
	 * @param internationalizable the internationalizable to set
	 */
	public void setInternationalizable(Internationalizable internationalizable) {
		this.internationalizable = internationalizable;
	}

	/**
	 * @return the extendedKey
	 */
	public String getExtendedKey() {
		return extendedKey;
	}

	/**
	 * @param extendedKey the extendedKey to set
	 */
	public void setExtendedKey(String extendedKey) {
		this.extendedKey = extendedKey;
	}

	public String getLanguageName() {
		return this.getInternationalizer().getName();
	}

	public Boolean isForLanguage(String languageName) {
		return this.getLanguageName() != null && this.getLanguageName().equalsIgnoreCase(languageName);
	}

	public void setLanguageName(String internationalizerName) {
		//TODO ver como implementarlo.
	}

	public String getKey() {
		int indexBeginLanguaje = this.getExtendedKey().indexOf(this.getLanguageName().toUpperCase());
		String key = this.getExtendedKey().substring(indexBeginLanguaje);
		return key.toUpperCase();
	}

	public String getValue() {
		return this.getText();
	}

	public Boolean isForKey(Object key) {
		return this.getKey().equalsIgnoreCase(key.toString());
	}
}
